<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Policy extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->config->load('zipcode');
		$this->lang->load('zipcode', 'english');
		$this->load->model('ZipcodeModel');
	}
	public function index()
	{
		$data = array();
		$zipcodeData = $this->_checkDomain();
		
		if($zipcodeData){			
			$data['zipcodeData'] = $zipcodeData;
			$this->load->view('policy', $data);
		}else{
			$bgImagesPath = $this->config->item('background_images_path');
			$bgImages = scandir($bgImagesPath);
			$data['bgImagesPath'] = $bgImagesPath;
			$data['bgImages'] = $bgImages;
			
			$this->load->view('index', $data);
		}
		
		
		
	}
	private function _checkDomain(){
		$base_url = base_url();
		if(strpos($base_url, 'www.')){
			$zipcode = substr($base_url, strpos($base_url, 'www.') + 4, 5);
		}else{
			$zipcode = substr($base_url, strpos($base_url, '//') + 2, 5);
		}
		
		$zipcodeData = $this->ZipcodeModel->getByZipcode($zipcode);
		if($zipcodeData){
			return $zipcodeData;
		}else{
			if(!strpos($base_url, 'zipcode')){
				header("HTTP/1.1 301 Moved Permanently");
				header("Location: http://zipcode.us");
			}else{
				return null;
			}
		}
	}
}